/**
 * author:Administrator
 * date:2016/1/4
 * desc: koa-better-body in router
 */

'use strict';

var bodyParser = require('koa-better-body');
var koaBody = bodyParser({});

var Router = require('koa-router');
var __pathPrefix = '/books';
var router = new Router({prefix: __pathPrefix});
module.exports = router.routes();

var biz = require('../biz/book.js');
/**
 * post /books
 {
   "name": "book1",
   "uid": xx
 }
 */
router.post('/', koaBody, function*() {
  var json = {
    name: this.request.body.fields.name,
    uid: this.request.body.fields.uid
  };
  console.log(json);
  var retObj = yield biz.create(json);
  console.log(retObj);
  this.body = retObj;
});

/**
 * get book info
 * get /books/:id
 */
router.get('/:id', function*() {
  var id = this.params.id;
  var retObj = yield  biz.get(id);
  console.log(retObj);
  this.body = retObj;
});

// edit delete